package com.wonderful.wedding.api;

import com.google.common.base.Optional;
import com.wonderful.wedding.model.MyCloudResult;
import com.wonderful.wedding.model.Person;
import com.wonderful.wedding.service.FaceService;
import org.apache.tomcat.util.http.fileupload.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@RestController
@RequestMapping("/face")
@SuppressWarnings("unchecked")
public class FaceController {

    @Autowired
    private FaceService faceService;


    @Value("${images}")
    private String imagesPath;

    @PostMapping("/person")
    public ResponseEntity createPerson(@RequestParam(value = "image") MultipartFile image,
                                       @RequestParam(value = "person_name") String personName,
                                       @RequestParam(value = "group_name") String groupName) {


        try {
            File imageFile = new File(imagesPath + image.getOriginalFilename());
            image.transferTo(imageFile);

            MyCloudResult myCloudResult = faceService.createPersonWithImage(personName, groupName, imageFile);

            if (imageFile.exists()) {
                FileUtils.forceDelete(imageFile);
            }

            return MyCloudResult.parseToEntity(myCloudResult);
        } catch (IOException e) {
            e.printStackTrace();
            return new ResponseEntity(e.getMessage(), HttpStatus.BAD_REQUEST);
        }
    }

    @PostMapping("/identify")
    public ResponseEntity identifyPerson(@RequestParam("image") MultipartFile image,
                             @RequestParam(value = "group_name") String groupName) {
        try {
            File imageFile = new File(imagesPath + image.getOriginalFilename());
            image.transferTo(imageFile);

            Optional<Person> person = faceService.identify(imageFile, groupName);

            if (imageFile.exists()) {
                FileUtils.forceDelete(imageFile);
            }

            if (person.isPresent()) {
                return new ResponseEntity(person.get(), HttpStatus.OK);
            } else {
                return new ResponseEntity("person isn't exist", HttpStatus.BAD_REQUEST);
            }

        } catch (IOException e) {
            e.printStackTrace();
            return new ResponseEntity(HttpStatus.BAD_REQUEST);
        }

    }

    @GetMapping("/ping")
    public ResponseEntity ping() {
        return new ResponseEntity("pang", HttpStatus.OK);
    }
}
